package org.nw.web.sys.common;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.nw.exception.BusiException;
import org.nw.service.sys.SmsService;
import org.nw.vo.sys.SmsVO;
import org.nw.web.AbsToftController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 站内信服务
 * 
 * @author xuqc
 * @date 2013-7-1 上午11:30:41
 */
@Controller
@RequestMapping(value = "/common/sms")
public class SmsController extends AbsToftController {

	@Autowired
	private SmsService smsService;

	@Override
	public SmsService getService() {
		return smsService;
	}

	/**
	 * 查看动作，跳转到查看页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getByPK.json")
	@ResponseBody
	public Map<String, Object> getByPK(HttpServletRequest request, HttpServletResponse response) {
		String pk_sms = request.getParameter("pk_sms");
		if(StringUtils.isBlank(pk_sms)) {
			throw new BusiException("主键不能为空！");
		}
		this.getService().updateReadFlag(pk_sms);
		return this.genAjaxResponse(true, null, this.getService().getByPK(pk_sms));
	}

	/**
	 * 发送消息
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/send.json")
	@ResponseBody
	public Map<String, Object> send(HttpServletRequest request, HttpServletResponse response) {
		String receiver = request.getParameter("receiver");// 如果是多个receiver使用逗号分开
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		if(StringUtils.isBlank(title)) {
			throw new BusiException("站内信的标题不能为空！");
		}

		SmsVO vo = new SmsVO();
		vo.setReceiver(receiver);
		vo.setTitle(title);
		vo.setContent(content);
		this.getService().doSend(vo);
		return this.genAjaxResponse(true, null, null);
	}
}
